package UIGame;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;

import client.Client;

import Sever.Server;
import Sever.tbaleGame;

import java.awt.event.ActionEvent;
import java.awt.image.TileObserver;
import java.util.Vector;

import libarary.Contants;

public class IUGame implements Contants{
	private JSplitPane split, jsplitchat;
	private JTabbedPane jtab;
	JList listUser,listchat;
	Client cl;
	Vector<String> v = new Vector<String>(2, 3);
	Vector <String> vchat=new Vector<String>(1,2);
	public IUXepGach iuxg;
	Vector <tbaleGame> listban=new Vector <tbaleGame>(1,2);
	public IUGame(Vector v,Client cl) {
		this.v = v;
		this.cl=cl;
		jsplitchat = new JSplitPane(JSplitPane.VERTICAL_SPLIT, UiList(),
				Uichat());
		jsplitchat.setBorder(BorderFactory.createEmptyBorder());
		jsplitchat.setBackground(Color.decode("#008B8B"));
		jsplitchat.setDividerSize(0);
		split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, lefmenu(),
				JpnCenter());
		
		split.setBackground(Color.decode("#008B8B"));
		split.setBorder(BorderFactory.createEmptyBorder());
		split.setDividerSize(0);
	}

	public JSplitPane getJSplitPane() {
		return split;
	}

	public JSplitPane getjsplitchat() {
		return jsplitchat;
	}

	public JTabbedPane getjtab() {
		return jtab;
	}

	public void setVectorListUser(Vector <String>v) {
		this.v = v;
	}
	public void setVectorListChat(Vector <String>v) {
		this.vchat = v;
	}
	public void addVectoListChat(String st){
		vchat.add(st);
	}
	public static JPanel creatJPanel(JPanel jp) {
		JPanel newjp = new JPanel();
		newjp.setBackground(Color.decode("#008B8B"));
		newjp.add(jp);
		return newjp;

	}

	public JPanel GameImage(String Gamename, String Url) {
		JPanel Lg = new JPanel(new BorderLayout());
		Lg.setBackground(Color.decode("#E0FFFF"));
		JPanel jpS = new JPanel();
		jpS.setBackground(Color.decode("#E0FFFF"));
		JPanel logoPane = new JPanel(new BorderLayout());
		logoPane.add(new JLabel(new ImageIcon(Url)), BorderLayout.WEST);
		JPanel TextAndButon = new JPanel(new GridLayout(3,1));
		TextAndButon.setBackground(Color.decode("#E0FFFF"));
		TextAndButon.add(new JLabel(Gamename, JLabel.LEFT));
		TextAndButon.add(jpS);
		JPanel AndButon = new JPanel(new GridLayout(1,2));
		JPanel jp=new JPanel(); 
		jp.setBackground(Color.decode("#E0FFFF"));
		AndButon.add(jp);
		final JButton JBchoi = new JButton("Ch\u01a1i");
		JBchoi.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if (e.getSource() == JBchoi) {
					
					iuxg = new IUXepGach(cl);
					//jtab.addTab("Ph\u00f2ng game X\u1ebfp g\u1ea1ch",phonggame());
					cl.sendData(state.gamescore+"");
					iuxg.playgame();
					
					//updateUSer();
				}
			}

		});
		AndButon.add(JBchoi);
		TextAndButon.add(AndButon);
		Lg.add(logoPane, BorderLayout.WEST);
		Lg.add(TextAndButon, BorderLayout.EAST);
		Lg.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
		return Lg;
	}

	public JTabbedPane lefmenu() {
		JTabbedPane jtab=new JTabbedPane();
		JScrollPane jScrollPane = new JScrollPane();
		jScrollPane
				.setViewportView(creatJPanel(GameImage(
						"X\u1ebfp h\u00ecnh",
						".//picture//xepgach.png")));
		jScrollPane
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		jScrollPane
				.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		jtab.addTab("Danh s\u00e1ch game", jScrollPane);
		return jtab;
	}
	
	public JTabbedPane Uichat() {
		//JPanel Lg=new JPanel(new BorderLayout());
		JTabbedPane jtab=new JTabbedPane();
		jtab.setBackground(Color.decode("#008B8B"));
		JPanel Lg = new JPanel(new BorderLayout());
		Lg.setBackground(Color.decode("#008B8B"));
		
		listchat=new JList(vchat);
		listchat.setBackground(Color.decode("#E0FFFF"));
		JScrollPane ChatScrollPane = new JScrollPane(listchat);
		ChatScrollPane
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		ChatScrollPane
				.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		
		final JTextField JTchat = new JTextField();
		final JButton JBgoi = new JButton("G\u1edfi");
		
		JBgoi.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if (e.getSource() == JBgoi) {
					if(JTchat.getText().equals("")!=true){
						cl.sendData(state.chat+cl.nameuser+": "+JTchat.getText());
					}
				}
				
			}
		});
		JPanel chatNouth = new JPanel(new BorderLayout());
		chatNouth.setBackground(Color.decode("#008B8B"));
		chatNouth.add(JTchat, BorderLayout.CENTER);
		chatNouth.add(JBgoi, BorderLayout.EAST);
		Lg.add(ChatScrollPane, BorderLayout.CENTER);
		Lg.add(chatNouth, BorderLayout.SOUTH);
		jtab.addTab("chatRoom", Lg);
		return jtab;
	}

	public JTabbedPane UiList() {
		JTabbedPane jtab=new JTabbedPane();
		
		listUser = new JList(v);
		listUser.setBackground(Color.decode("#E0FFFF"));
		JScrollPane listScrollPane = new JScrollPane(listUser);
		listScrollPane.setBackground(Color.decode("#008B8B"));
		listScrollPane
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		listScrollPane
				.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		jtab.addTab("Danh S\u00e1ch \u0111ang ch\u01a1i", listScrollPane);
		
		return jtab;
	}
	//-------------------------------------------------
	public void updateUSer(){
		jsplitchat.setLeftComponent(UiList());
	}
	public void updateListChat(){
		jsplitchat.setRightComponent(Uichat());
	}
	public JTabbedPane JtCenter() {
		jtab = new JTabbedPane();
		jtab.setBackground(Color.decode("#008B8B"));
		JPanel logoPane = new JPanel(new BorderLayout());
		logoPane.setBackground(Color.decode("#008B8B"));
		JPanel Title = new JPanel(new GridLayout(2, 1));
		JPanel Tcenter = new JPanel(new FlowLayout(FlowLayout.CENTER));
		JLabel dhbk = new JLabel("\u0110\u1ea0I H\u1ECCC B\u00c1CH KHOA");
		dhbk.setFont(new Font("sansserif", Font.BOLD, 24));
		Tcenter.add(dhbk);
		JPanel Kcenter = new JPanel(new FlowLayout(FlowLayout.CENTER));
		JLabel khoa = new JLabel("KHOA C\u00d4NG NGH\u1ec6 TH\u00d4NG TIN");
		khoa.setFont(new Font("sansserif", Font.BOLD, 24));
		Kcenter.add(khoa);
		Title.add(Tcenter);
		Title.add(Kcenter);

		JPanel noidung = new JPanel(new GridLayout(5, 1));
		JPanel mleft = new JPanel(new FlowLayout(FlowLayout.CENTER));
		JLabel monhoc = new JLabel(
				"\u0110\u1ed2 \u00c1N L\u1EACP TR\u00ccNH M\u1ea0NG");
		monhoc.setFont(new Font("sansserif", Font.BOLD, 32));
		mleft.add(monhoc);
		JPanel left = new JPanel(new FlowLayout(FlowLayout.CENTER));
		JLabel detai1 = new JLabel("\u0110\u1ec1 t\u00e0i:");
		detai1.setFont(new Font("sansserif", Font.ITALIC, 18));
		JLabel detai = new JLabel("GAME M\u1ea0NG LAN");
		detai.setFont(new Font("sansserif", Font.BOLD, 24));
		left.add(detai1);
		left.add(detai);

		JPanel center = new JPanel(new FlowLayout(FlowLayout.CENTER));
		JLabel GVhd = new JLabel("GVHD:");
		GVhd.setFont(new Font("sansserif", Font.ITALIC, 18));
		JLabel GVhd1 = new JLabel("NGUY\u1ec4N V\u00d5 QUANG \u0110\u00d4NG");
		GVhd1.setFont(new Font("sansserif", Font.PLAIN, 18));
		center.add(GVhd);
		center.add(GVhd1);
		JPanel NameSV = new JPanel(new FlowLayout(FlowLayout.CENTER));
		JLabel SV = new JLabel("SV:");
		SV.setFont(new Font("sansserif", Font.ITALIC, 18));
		JLabel SV1 = new JLabel("H\u1ed2 NG\u1ECCC DUY");
		SV1.setFont(new Font("sansserif", Font.PLAIN, 18));
		NameSV.add(SV);
		NameSV.add(SV1);
		noidung.add(mleft);
		noidung.add(left);
		noidung.add(center);
		noidung.add(NameSV);
		logoPane.add(Title, BorderLayout.NORTH);
		logoPane.add(noidung, BorderLayout.CENTER);

		jtab.addTab("Th\u00f4ng tin", logoPane);
		return jtab;
	}

	public JPanel JpnCenter() {

		JPanel Jpcenter = new JPanel(new BorderLayout());
		Jpcenter.setBackground(Color.decode("#008B8B"));
		Jpcenter.add(JtCenter(), BorderLayout.CENTER);
		// Jpcenter.add(listAndChat(), BorderLayout.EAST);
		Jpcenter.add(jsplitchat, BorderLayout.EAST);
		return Jpcenter;
	}
	//phong game
	public JScrollPane phonggame(){
		
		JPanel Jpcenter = new JPanel(new GridLayout(0,4,5,5));
		
		for(int i=0;i<100;i++){
			tbaleGame tbg=new tbaleGame(i);
			listban.add(tbg);
			Jpcenter.add(tbg.tabgame());
		}
		JScrollPane listScrollPane = new JScrollPane(Jpcenter );
		listScrollPane.setBackground(Color.decode("#008B8B"));
		listScrollPane
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		listScrollPane
				.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		return listScrollPane;
	}
}
